package com.wxjfkg.workflow.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fivestar.util.str.StringUtils;
import com.fivestar.workflow.ActPtaskAction;
import com.fivestar.workflow.XbkProcessService;
import com.fivestar.workflow.XbkWorkOrderTaskService;
import com.fivestar.xbk.service.vo.XbkConstants;
import com.fivestar.xbk.service.vo.XbkWorkOrderTaskDto;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxjfkg.workflow.dao.XbkWorkOrderTaskDao;
import com.wxjfkg.ywxt.domain.loan.apply.BaseProcessVO;
import com.wxjfkg.ywxt.domain.loan.apply.ProcessFormDto;

@Service("xbkWorkOrderTaskService")
public class XbkWorkOrderTaskServiceImpl implements XbkWorkOrderTaskService {
	
	@Autowired
	private XbkWorkOrderTaskDao xbkWorkOrderTaskDao;
    @Autowired
    private XbkProcessService xbkProcessService;
	
	@Override
	public void saveXbkPersonalTasksVO(XbkWorkOrderTaskDto personalTask) {
		xbkWorkOrderTaskDao.saveXbkPersonalTasksVO(personalTask);
	}

	@Override
	public PageInfo<XbkWorkOrderTaskDto> selectPersonalTaskPager(
			XbkWorkOrderTaskDto queryForm) {
		PageHelper.startPage(queryForm.getPageNum(), queryForm.getPageSize());
		queryForm.setTaskStatus(XbkConstants.TASK_STATUS.TO_DO);
		List<XbkWorkOrderTaskDto> taskDtos=xbkWorkOrderTaskDao.queryPersonalTask(queryForm);
		PageInfo<XbkWorkOrderTaskDto> pageInfo=new PageInfo<XbkWorkOrderTaskDto>(taskDtos);
		return pageInfo;
	}

	public void insertActPtaskAction(List<ActPtaskAction> actions) {
		xbkWorkOrderTaskDao.insertActPtaskAction(actions);
	}

	@Override
	public void completeActPersonalTasksVO(XbkWorkOrderTaskDto zPersonalTasks) {
		xbkWorkOrderTaskDao.completeActPersonalTasksVO(zPersonalTasks);
	}

	@Override
	public XbkWorkOrderTaskDto selectXbkWorkOrderTaskDto(
			XbkWorkOrderTaskDto zPersonalTasks) {
		return xbkWorkOrderTaskDao.selectXbkWorkOrderTaskDto(zPersonalTasks);
	}

	@Override
	public void deleteActPersonalTasksVO(XbkWorkOrderTaskDto deleteTask) {
		xbkWorkOrderTaskDao.deleteActPersonalTasksVO(deleteTask);
	}

	@Override
	public List<ActPtaskAction> selectXbkWorkOrderTaskAction(XbkWorkOrderTaskDto task) {
		return xbkWorkOrderTaskDao.selectXbkWorkOrderTaskAction(task);
	}

	@Override
	public boolean isPersonalTaskExists(XbkWorkOrderTaskDto personalTask) {
		return xbkWorkOrderTaskDao.isPersonalTaskExists(personalTask)>=1;
	}

	@Override
	public PageInfo<XbkWorkOrderTaskDto> selectPersonalTaskDonePager(XbkWorkOrderTaskDto queryForm) {
		PageHelper.startPage(queryForm.getPageNum(), queryForm.getPageSize());
		queryForm.setTaskStatus(XbkConstants.TASK_STATUS.DONE);
		List<XbkWorkOrderTaskDto> taskDtos = xbkWorkOrderTaskDao.queryPersonalTask(queryForm);
		PageInfo<XbkWorkOrderTaskDto> pageInfo = new PageInfo<XbkWorkOrderTaskDto>(taskDtos);
		return pageInfo;
	}

	@Override
	public XbkWorkOrderTaskDto selectTaskByPk(XbkWorkOrderTaskDto xbkWorkOrderTaskDto) {
		return xbkWorkOrderTaskDao.selectTaskByPk(xbkWorkOrderTaskDto);
	}

	@Override
	public void saveProcessForm(ProcessFormDto from) {
		xbkWorkOrderTaskDao.saveProcessForm(from);
	}

	@Override
	public ProcessFormDto selectTaskProcessForm(ProcessFormDto form) {
		return xbkWorkOrderTaskDao.selectTaskProcessForm(form);
	}

	@Override
	public void setFormInputValue(ProcessFormDto form) {
		xbkWorkOrderTaskDao.setFormInputValue(form);
	}

	@Override
	public List<ProcessFormDto> selectTaskProcessFormInput(ProcessFormDto form) {
		return xbkWorkOrderTaskDao.selectTaskProcessFormInput(form);
	}

//	@Transactional(value="txTranManager",propagation = Propagation.REQUIRED)
	public void completeTask(String userName, Map<String, Object> variables,
			List<String> operIds, BaseProcessVO baseProcess) throws Exception {
		//2.2更新审批信息
		boolean update = true;
		if(variables!=null&&!variables.isEmpty()){
			String inputValue = (String) variables.get("inputValue");
			ProcessFormDto form = new ProcessFormDto();
	        form.setTaskId(baseProcess.getTk_id());
	        form.setInputValue(inputValue);
	        form.setBussnessKey(baseProcess.getBusinessKey());
	        form.setInputer(userName);
	        setFormInputValue(form);
	        
	        form = selectTaskProcessForm(form);
	        if(StringUtils.isNotEmpty(inputValue)&&form!=null
	        		&&StringUtils.isEmpty((String)variables.get(form.getId()))){
	        	//设置变量
	        	variables.put(form.getId() , inputValue);
	        }
	        ProcessFormDto pfd = selectTaskProcessForm(form);
	        update = updateBussinessFormValue(pfd);
		}
		if(!update){
			throw new Exception("更新审批信息失败!");
		}
		//3.3完成任务
		xbkProcessService.completeTask(userName, 
									   baseProcess.getTk_id(), 
									   operIds,
									   variables,
									   baseProcess);
		
	}

	@Override
	public boolean updateBussinessFormValue(ProcessFormDto form) {
		if(form != null){
			if(StringUtils.isNotEmpty(form.getFieldName())
				&&StringUtils.isNotEmpty(form.getPkField())
				&&StringUtils.isNotEmpty(form.getTableName())){
				StringBuffer sql = new StringBuffer();
				sql.append(" UPDATE ").append(form.getTableName()).append(" SET ").append(form.getFieldName()).append(" = ").append(form.getInputValue())
				   .append(" WHERE ").append(form.getPkField()).append(" = '").append(form.getBussnessKey()).append("'");
				Map<String ,String> sqlMap = new HashMap<String , String>();
				sqlMap.put("sql", sql.toString());
				return xbkWorkOrderTaskDao.updateBySql(sqlMap) >= 1;
			}
		}
		return true;
	}

	@Override
	public boolean isPersonalTaskExistsUserIdAndTaskId(
			XbkWorkOrderTaskDto personalTask) {
		return xbkWorkOrderTaskDao.isPersonalTaskExistsUserIdAndTaskId(personalTask)>=1;
	}

}
